import 'dart:math';

import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame/game.dart';
import 'package:flame/palette.dart';
import 'package:flame/sprite.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:hero_back/core/extension/sprite_sheet_ext.dart';
import 'package:hero_back/study/flutter_game_flame/day001/component.dart';

class MyGame extends FlameGame
    with
        HasDraggables,
        KeyboardEvents,
        PanDetector,
        HasTappables,
        // TapDetector,
        HasHoverables {
  late final GirlComponent girl;
  late final JoystickComponent joystick;
  late Monster monster;
  final double step = 10;
  late double count = 0;

  @override
  Future<void> onLoad() async {
    final knobPaint = BasicPalette.blue.withAlpha(200).paint();
    final bGPaint = BasicPalette.blue.withAlpha(100).paint();
    joystick = JoystickComponent(
      knob: CircleComponent(radius: 25, paint: knobPaint),
      background: CircleComponent(radius: 50, paint: bGPaint),
      margin: EdgeInsets.only(left: 40, bottom: 40),
    );
    girl = GirlComponent();
    add(girl);
    add(joystick);

    ///动画帧图
    const String src = 'girl/girlCombine.png';
    await images.load(src);
    var image = images.fromCache(src);
    SpriteSheet sheet =
        SpriteSheet.fromColumnsAndRows(image: image, columns: 8, rows: 4);
    List<Sprite> sprites = List.generate(32, sheet.getSpriteById);
    // List<Sprite> sprites = sheet.getRowSprites(row: 0, count: 8);
    // List<Sprite> sprites = sheet.getRowSprites(row: 1, count: 8);
    // List<Sprite> sprites = sheet.getRowSprites(row: 2, count: 8);
    // List<Sprite> sprites = sheet.getRowSprites(row: 3, count: 8);
    SpriteAnimation animation =
        SpriteAnimation.spriteList(sprites, stepTime: 1 / 8, loop: true);
    // SpriteAnimation animation01 = SpriteAnimation.fromFrameData(
    //   image,
    //   SpriteAnimationData.sequenced(
    //     amount: 32,
    //     amountPerRow: 8,
    //     stepTime: 1 / 8,
    //     textureSize: Vector2(
    //       image.width / 8,
    //       image.height / 4,
    //     ),
    //   ),
    // );
    // Sprite sprite = sheet.getSpriteById(0);
    Vector2 monsterSize = Vector2(164, 164);
    final double pY = Random().nextDouble() * size.y;
    final double pX = size.x - monsterSize.x / 2;
    // monster = Monster(sprite, monsterSize, Vector2(pX, pY));
    monster = Monster(animation, monsterSize, Vector2(pX, pY));
    add(monster);
  }

  @override
  void update(double dt) {
    super.update(dt);

    ///移动
    if (!joystick.delta.isZero()) {
      Vector2 ds = joystick.relativeDelta * girl.speed * dt;
      girl.move(ds);
    }

    ///旋转
    if (!joystick.delta.isZero()) {
      girl.rotateTo(joystick.delta.screenAngle());
    } else {
      // girl.rotateTo(0);
    }
  }

  ///键盘按键
  @override
  KeyEventResult onKeyEvent(
      RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
    final isKeyDown = event is RawKeyDownEvent;
    if (event.logicalKey == LogicalKeyboardKey.keyY && isKeyDown) {
      girl.flip(y: true);
    }
    if (event.logicalKey == LogicalKeyboardKey.keyX && isKeyDown) {
      girl.flip(x: true);
    }

    ///键盘移动
    if (event.logicalKey == LogicalKeyboardKey.arrowUp ||
        event.logicalKey == LogicalKeyboardKey.keyW) {
      girl.move(Vector2(0, -step));
    }
    if (event.logicalKey == LogicalKeyboardKey.arrowDown ||
        event.logicalKey == LogicalKeyboardKey.keyS) {
      girl.move(Vector2(0, step));
    }
    if (event.logicalKey == LogicalKeyboardKey.arrowLeft ||
        event.logicalKey == LogicalKeyboardKey.keyA) {
      girl.move(Vector2(-step, 0));
    }
    if (event.logicalKey == LogicalKeyboardKey.arrowRight ||
        event.logicalKey == LogicalKeyboardKey.keyD) {
      girl.move(Vector2(step, 0));
    }
    return super.onKeyEvent(event, keysPressed);
  }

  ///点击动作
  ///TapDetector
  // @override
  // void onTapUp(TapUpInfo info) {
  //   box.debugMode = false;
  // }
  //
  // @override
  // void onTapDown(TapDownInfo info) {
  //   box.debugMode = true;
  // }
  //
  // @override
  // void onTapCancel() {
  //   box.debugMode = true;
  // }

  // @override
  // void onTap() {
  //   count++;
  //   girl.rotateTo(count * pi / 2);
  // }

  ///拖拽
  // @override
  // void onPanCancel() {
  //   box.debugMode = false;
  // }

  // @override
  // void onPanEnd(DragEndInfo info) {
  //   box.debugMode = false;
  // }

  // @override
  // void onPanUpdate(DragUpdateInfo info) {
  //   girl.move(info.delta.global);
  // }
  //
  // @override
  // void onPanStart(DragStartInfo info) {}

// @override
// void onPanDown(DragDownInfo info) {
//   box.debugMode = true;
// }
}
